// src/router/index.js
import {createRouter, createWebHistory} from "vue-router";
import { getToken } from "@/util/auth";

export const Layout = () => import("@/layout/index.vue");

// Import all routes except index.js
const files = import.meta.glob("./*.js", { eager: true });
const otherRoutes = Object.keys(files).reduce((routes, modulePath) => {
    if (modulePath === "./index.js" || modulePath === "./permission.js") return routes;
    const value = files[modulePath];
    if (value.default && Array.isArray(value.default)) {
        routes.push(...value.default);
    }
    return routes;
}, []);

export const constantRoutes = [
    {
        path: "/",
        name: "Home",
        component: Layout,
        // redirect: "/dashboard",
        meta: {
            title: "控制台",
            icon: "HomeFilled",
            affix: true,
            keepAlive: true,
            alwaysShow: false,
        },
        children: [
            {
                path: "/dashboard",
                name: "Dashboard",
                component: () => import("@/pages/Home.vue"),
                meta: {
                    title: "控制台",
                    icon: "Position",
                    affix: true,
                    keepAlive: true,
                    alwaysShow: false,
                },
            },
        ],
    },
    ...otherRoutes,
    {
        // login
        path: "/login",
        name: "Login",
        component: () => import("@/pages/Login.vue"),
    },
    {
        // 404 page must be placed at the end !!!
        path: "/:pathMatch(.*)*",
        name: "NotFound",
        component: () => import("@/pages/404.vue"),
    },
];

const router = createRouter({
    history: createWebHistory(),
    routes: constantRoutes,
});

export default router;
